package com.qf.common.core.common;

/**
 * 天--道--酬--勤
 *
 * @author LiangGaoQiang
 * @ClassName R
 * @Date: 2025/4/3 20:37
 * @Description: 描述
 */

import com.qf.common.core.constants.CommonConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 封装返回结果的工具类
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class R {
    private int code;
    private String msg;  // 信息描述 例如：成功、失败、参数错误等
    private Object data;  // 返回的数据 例如：用户信息、订单信息等

    public static R ok() {
        R r = new R();
        r.setCode(CommonConstants.SUCCESS);
        r.setMsg(CommonConstants.SUCCESS_MESSAGE);
        return r;
    }

    public static R ok(Object data) {
        R r = new R();
        r.setCode(CommonConstants.SUCCESS);
        r.setMsg(CommonConstants.SUCCESS_MESSAGE);
        r.setData(data);
        return r;
    }

    public static R fail(Integer code, String msg) {
        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static R fail(String msg) {
        R r = new R();
        r.setCode(CommonConstants.FAIL);
        r.setMsg(msg);
        return r;
    }

    public static R fail() {
        R r = new R();
        r.setCode(CommonConstants.FAIL);
        r.setMsg(CommonConstants.FAIL_MESSAGE);
        return r;
    }

    public static R error(String defaultMessage) {
        R r = new R();
        r.setCode(CommonConstants.ERROR);
        r.setMsg(defaultMessage);
        return r;
    }
}
